<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="5" :md="12">
            <el-form-item label="设备类型:">
              <el-select
                clearable
                v-model="where.device_type"
                placeholder="请选择设备类型"
              >
                <el-option
                  v-for="item in device_types"
                  :key="item.value"
                  :label="item.label"
                  :value="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
              >
                <el-option value="1" label="在线"></el-option>
                <el-option value="2" label="离线"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="井号:">
              <el-input
                clearable
                v-model="where.name"
                placeholder="请输入井号"
              />
            </el-form-item>
          </el-col>

          <el-col :lg="5" :md="12">
            <el-form-item label="设备编号:">
              <el-input
                clearable
                v-model="where.device_no"
                placeholder="请输入设备编号"
              />
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格  -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 365px)"
        size="small"
        :page-size="20"
        :page-sizes="[20, 30, 40, 50, 100, 200]"
        :cell-style="cellStyle"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:device:add')"
            >添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:device:dall')"
            >删除
          </el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-edit"
            class="ele-btn-icon"
            @click="editBatch"
            v-if="permission.includes('sys:device:edit')"
            >修改
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link type="primary" :underline="false" @click="goStatusInfo(row)"
            >状态信息
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            @click="openDeviceControl(row)"
            >设备控制
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            @click="openDeviceInfo(row)"
            >设备信息
          </el-link>
          <el-link type="primary" :underline="false" @click="goHistory(row)"
            >历史数据
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            @click="openVideoMonitoring(row)"
            >视频监控
          </el-link>
          <el-link type="primary" :underline="false" @click="goAlarmRules(row)"
            >报警规则
          </el-link>
        </template>

        <!--  操作列新-->
        <template slot="action2" slot-scope="{ row }">
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="goStatusInfo(row)"
                  >状态信息
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openDeviceControl(row)"
                  >设备控制
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openDeviceInfo(row)"
                  >设备信息
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="goHistory(row)"
                  >历史数据
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openVideoMonitoring(row)"
                  >视频监控
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="goAlarmRules(row)"
                  >报警规则
                </el-link>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>

        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            @change="status(row)"
            :active-value="1"
            disabled
            :inactive-value="2"
          />
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 添加设备弹窗 -->
    <device-edit :data="current" :visible.sync="showEdit" @done="reload" />

    <!-- 设备信息弹窗 -->
    <device-info :data="current" :visible.sync="showDeviceInfo" />

    <!-- 设备控制弹窗 -->
    <device-control
      :data="current"
      :visible.sync="showDeviceControl"
      @done="reload"
    />

    <!-- 视频监控弹窗 -->
    <el-dialog
      title="视频监控"
      :visible.sync="showVideoMonitoring"
      width="50%"
      :destroy-on-close="true"
      :lock-scroll="false"
    >
      <el-form :inline="true" class="demo-form-inline" label-width="80px">
        <el-form-item label="设备名称:" prop="jk_device_name">
          <el-input
            v-model="jk_device_name"
            placeholder="请输入设备名称"
            disabled
          />
        </el-form-item>

        <el-form-item label="设备编号:" prop="jk_device_no">
          <el-input
            v-model="jk_device_no"
            placeholder="请输入设备编号"
            disabled
          />
        </el-form-item>
      </el-form>

      <div style="margin:0 10px">
        <iframe
          :src="jkSrc"
          width="100%"
          height="400"
          id="ysOpenDevice"
          allowfullscreen
        >
        </iframe>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import DeviceInfo from "./device-info";
import DeviceControl from "./device-control";
import DeviceEdit from "@/views/device/device-edit";

export default {
  name: "ExamDevice",
  components: { DeviceEdit, DeviceInfo, DeviceControl },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/device/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center"
          // fixed: "left"
        },
        {
          prop: "id",
          label: "ID",
          width: 60,
          align: "center",
          sortable: "custom",
          showOverflowTooltip: true
          // fixed: "left",
        },
        {
          prop: "device_no",
          label: "设备编号",
          showOverflowTooltip: true,
          minWidth: 100,
          align: "center"
        },
        {
          prop: "name",
          label: "井号",
          showOverflowTooltip: true,
          minWidth: 100,
          align: "center"
        },
        {
          prop: "device_type",
          label: "设备类型",
          showOverflowTooltip: true,
          minWidth: 100,
          align: "center"
        },
        {
          prop: "address",
          label: "安装位置",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "status",
          label: "状态",
          align: "center",
          width: 80,
          resizable: false,
          slot: "status"
        },
        {
          prop: "liuya",
          label: "流压(MPa)",
          showOverflowTooltip: true,
          minWidth: 90,
          align: "center"
        },
        {
          prop: "biao_liuya",
          label: "校准流压(MPa)",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center"
        },
        {
          prop: "jingdi_temp",
          label: "井底温度(℃)",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center"
        },
        {
          prop: "taoya",
          label: "套压(MPa)",
          showOverflowTooltip: true,
          minWidth: 90,
          align: "center"
        },
        {
          prop: "yezhu_high",
          label: "液柱高度(m)",
          showOverflowTooltip: true,
          minWidth: 100,
          align: "center"
        },
        {
          prop: "shunshi_qiliang",
          label: "瞬时气量(m³/h)",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center"
        },
        {
          prop: "leiji_qiliang",
          label: "累计产气量(m³/h)",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center"
        },
        {
          prop: "liuliang_temp",
          label: "流量计温度(℃)",
          showOverflowTooltip: true,
          minWidth: 120,
          align: "center"
        },
        {
          prop: "liuliang_yali",
          label: "流量计压力(kPa)",
          showOverflowTooltip: true,
          minWidth: 130,
          align: "center"
        },
        {
          prop: "pinlv_chongci",
          label: "转速(RPM)/冲次(次/分)",
          showOverflowTooltip: true,
          minWidth: 150,
          align: "center"
        },
        {
          prop: "pinlv",
          label: "频率(Hz)",
          showOverflowTooltip: true,
          minWidth: 90,
          align: "center"
        },
        {
          prop: "dianliu",
          label: "电流(A)",
          showOverflowTooltip: true,
          minWidth: 90,
          align: "center"
        },
        {
          prop: "dianya",
          label: "电压(V)",
          showOverflowTooltip: true,
          minWidth: 90,
          align: "center"
        },
        {
          prop: "chanshui",
          label: "产水(m³/h)",
          showOverflowTooltip: true,
          minWidth: 100,
          align: "center"
        },
        {
          prop: "create_time",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: "update_time",
          label: "更新时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: "action",
          label: "操作",
          width: 60,
          align: "center",
          resizable: false,
          slot: "action2"
          // fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示弹窗
      showEdit: false,
      showDeviceInfo: false,
      showDeviceControl: false,
      showVideoMonitoring: false,
      showAlarmRules: false,
      device_types: [
        { label: "杆式泵", value: "1" },
        { label: "螺杆泵", value: "2" },
        { label: "柱塞泵", value: "3" },
        { label: "管式泵", value: "4" }
      ],
      jkSrc: "",
      jk_device_name: "",
      jk_device_no: "",
      timer: null
    };
  },
  mounted() {
    // 10s秒刷新一次页面
    this.timer = setInterval(() => {
      this.reload();
    }, 10000);
  },
  destroyed(){
    if(this.timer) { //如果定时器在运行则关闭
      clearInterval(this.timer);
    }
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示状态信息 */
    goStatusInfo(row) {
      // this.$router.push({
      //   path: '/device/status-info',
      //   query: {device_no: row.device_no, device_id: row.id}
      //
      // })

      window.open(
        `/device/status-info?device_no=` +
          row.device_no +
          "&device_id=" +
          row.id +
          "&name=" + row.name
      );
    },
    /* 历史数据页面 */
    goHistory(row) {
      // this.$router.push({
      //   path: '/device/history',
      //   query: {device_no: row.device_no, device_id: row.id}
      // })

      window.open(
        `/device/history?device_no=` + row.device_no + "&device_id=" + row.id + "&name=" + row.name
      );
    },

    /* 显示设备信息 */
    openDeviceInfo(row) {
      this.current = row;
      this.showDeviceInfo = true;
    },
    /* 显示设备控制 */
    openDeviceControl(row) {
      this.current = row;
      this.showDeviceControl = true;
    },
    /* 显示视频监控 */
    openVideoMonitoring(row) {
      this.current = row;
      this.getJKUrl();
      this.showVideoMonitoring = true;
    },
    /* 获取监控地址 */
    getJKUrl() {
      console.log("getJKUrl", this.current);
      this.$http
        .post("/device/jkUrl", { device_id: this.current.id })
        .then(res => {
          if (res.data.code === 0) {
            this.jkSrc = res.data.data.url;
            this.jk_device_name = res.data.data.info.video_name;
            this.jk_device_no = res.data.data.info.video_no;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.$message.error(e.message);
        });
    },
    /* 显示报警规则 */
    goAlarmRules(row) {
      this.$router.push({
        path: "/device/alarm-rules",
        query: { device_no: row.device_no }
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/device/delete", { id: row.id })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 修改 */
    editBatch() {
      if (this.selection.length !== 1) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.current = this.selection[0];
      this.showEdit = true;
    },

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的设备吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/device/delete", { id: this.selection.map(d => d.id) })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status
      });
      this.$http
        .post("/device/status", params)
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    cellStyle({ row }) {
      if (row.abnormal === 1) {
        return "color:red";
      }
      return "";
    }
  }
};
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}

.el-icon-arrow-down {
  font-size: 12px;
}

.red-text {
  color: red;
}
</style>
